package com.src;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import com.src.R;

public class CharactersPopActivity extends Activity implements
		OnGestureListener {
	private TextView mTextView1, mTextView2, mTextView3, mTextView4,
			mTextView5, mTextView6, mTextView7, mTextView8, mTextView9,
			mTextView10, mTextView11, mTextView12, mTextView13;
	private Animation animation1, animation2, animation3, animation4,
			animation6, animation7, animation8, animation9, animation11,
			animation12, animation13, animation2_1, animation2_2, animation2_3,
			animation2_4, animation2_5, animation2_6, animation2_8,
			animation2_9, animation2_10, animation2_11, animation2_12,
			animation2_13;
	private GestureDetector gDetector;
	private String[] keywords = { "安卓优化大师", "飞信", "赛车", "360", "短信", "文件管理",
			"拨号", "短信", "捕鱼达人", "日历", "hd", "视频播放器", "3D动态壁纸" };

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.characters_float_layout);
		initAnimations();
		initAnimations2();
		initViews();
		randomTanslate();
		gDetector = new GestureDetector(this);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return gDetector.onTouchEvent(event);
	}

	private void initViews() {
		mTextView1 = (TextView) findViewById(R.id.txt1);
		mTextView2 = (TextView) findViewById(R.id.txt2);
		mTextView3 = (TextView) findViewById(R.id.txt3);
		mTextView4 = (TextView) findViewById(R.id.txt4);
		mTextView5 = (TextView) findViewById(R.id.txt5);
		mTextView6 = (TextView) findViewById(R.id.txt6);
		mTextView7 = (TextView) findViewById(R.id.txt7);
		mTextView8 = (TextView) findViewById(R.id.txt8);
		mTextView9 = (TextView) findViewById(R.id.txt9);
		mTextView10 = (TextView) findViewById(R.id.txt10);
		mTextView11 = (TextView) findViewById(R.id.txt11);
		mTextView12 = (TextView) findViewById(R.id.txt12);
		mTextView13 = (TextView) findViewById(R.id.txt13);

	}

	private void randomText() {
		List<String> list = new ArrayList<String>(Arrays.asList(keywords));
		for (int i = R.id.txt1; i < R.id.txt13; i++) {
			((TextView) findViewById(i)).setText(getKeyword(list));
		}
	}

	private String getKeyword(List<String> list) {
		if (list != null && list.size() > 0) {
			int num = random.nextInt(list.size());
			String keyword = list.get(num);
			list.remove(num);
			return keyword;
		} else {
			return "";
		}
	}

	private void initAnimations() {
		animation1 = AnimationUtils.loadAnimation(this, R.anim.anim1);
		animation2 = AnimationUtils.loadAnimation(this, R.anim.anim2);
		animation3 = AnimationUtils.loadAnimation(this, R.anim.anim3);
		animation4 = AnimationUtils.loadAnimation(this, R.anim.anim4);
		animation6 = AnimationUtils.loadAnimation(this, R.anim.anim6);
		animation7 = AnimationUtils.loadAnimation(this, R.anim.anim7);
		animation8 = AnimationUtils.loadAnimation(this, R.anim.anim8);
		animation9 = AnimationUtils.loadAnimation(this, R.anim.anim9);
		animation11 = AnimationUtils.loadAnimation(this, R.anim.anim11);
		animation12 = AnimationUtils.loadAnimation(this, R.anim.anim12);
		animation13 = AnimationUtils.loadAnimation(this, R.anim.anim13);
	}

	private void initAnimations2() {
		// TranslateAnimation mAnimation2_1=new TranslateAnimation(0, 0,
		// fromYDelta, toYDelta)
		animation2_1 = AnimationUtils.loadAnimation(this, R.anim.anim2_1);
		animation2_2 = AnimationUtils.loadAnimation(this, R.anim.anim2_2);
		animation2_3 = AnimationUtils.loadAnimation(this, R.anim.anim2_3);
		animation2_4 = AnimationUtils.loadAnimation(this, R.anim.anim2_4);
		animation2_5 = AnimationUtils.loadAnimation(this, R.anim.anim2_5);
		animation2_6 = AnimationUtils.loadAnimation(this, R.anim.anim2_6);
		animation2_8 = AnimationUtils.loadAnimation(this, R.anim.anim2_8);
		animation2_9 = AnimationUtils.loadAnimation(this, R.anim.anim2_9);
		animation2_10 = AnimationUtils.loadAnimation(this, R.anim.anim2_10);
		animation2_11 = AnimationUtils.loadAnimation(this, R.anim.anim2_11);
		animation2_12 = AnimationUtils.loadAnimation(this, R.anim.anim2_12);
		animation2_13 = AnimationUtils.loadAnimation(this, R.anim.anim2_13);
	}

	private void startAnimations1() {
		mTextView1.startAnimation(animation1);
		mTextView2.startAnimation(animation2);
		mTextView3.startAnimation(animation3);
		mTextView4.startAnimation(animation4);
		mTextView5.startAnimation(animation4);
		mTextView6.startAnimation(animation6);
		mTextView7.startAnimation(animation7);
		mTextView8.startAnimation(animation8);
		mTextView9.startAnimation(animation9);
		mTextView10.startAnimation(animation9);
		mTextView11.startAnimation(animation11);
		mTextView12.startAnimation(animation12);
		mTextView13.startAnimation(animation13);
	}

	private void startAnimations2() {
		mTextView1.startAnimation(animation2_1);
		mTextView2.startAnimation(animation2_2);
		mTextView3.startAnimation(animation2_3);
		mTextView4.startAnimation(animation2_4);
		mTextView5.startAnimation(animation2_5);
		mTextView6.startAnimation(animation2_6);
		mTextView8.startAnimation(animation2_8);
		mTextView9.startAnimation(animation2_9);
		mTextView10.startAnimation(animation2_10);
		mTextView11.startAnimation(animation2_11);
		mTextView12.startAnimation(animation2_12);
		mTextView13.startAnimation(animation2_13);
	}

	private void startAnimations3() {

	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	private void randomTanslate() {
		int num = random.nextInt(2);
		randomText();
		switch (num) {
		case 0:
			startAnimations1();
			break;
		case 1:
			startAnimations2();
			break;
		}
	}

	private Random random = new Random();

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {

		randomTanslate();
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// System.out.println("onScroll");
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

}